package com.six.business.controller;

import com.github.pagehelper.PageInfo;
import com.six.business.service.BusinessService;
import com.six.business.vo.BusinessVo;
import com.six.core.entity.Business;
import com.six.core.util.JwtUtil;
import com.six.core.util.UploadUtil;
import com.six.core.util.WebUtil;
import com.six.core.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * @author: WangPeng
 * @createTime: 2023/02/27 15:54
 * @description:
 * @version: v1.0
 */
@Api(value = "/business/business", tags = {":"})
@RestController
@RequestMapping("/business/business")
public class BusinessController {
    @Autowired
    private BusinessService businessService;

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "currentPage", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "BusinessVo", name = "businessVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/findApproval")
    public Result findApproval ( int currentPage, int pageSize, @RequestBody BusinessVo businessVo){
        return businessService.findApproval(currentPage, pageSize, businessVo);
    }

    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "currentPage", value = "", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "pageSize", value = "", required = true),
            @ApiImplicitParam(paramType = "body", dataType = "BusinessVo", name = "businessVo", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/findUnreviewed")
    public Result findUnreviewed( int currentPage, int pageSize, @RequestBody BusinessVo businessVo){
        return businessService.findUnreviewed(currentPage,pageSize,businessVo);
    }

    /** 前台商家中心申请入驻商家 */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Business", name = "business", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/addBusiness")
    public Result addBusiness(@RequestBody Business business){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer userid = (Integer) map.get("tbUserid");
        business.setUserid(userid);
        return businessService.addBusiness(business);
    }

    /** 前台商家中心获取商家信息 */
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "int", name = "userid", value = "", required = true)
    })
    @PostMapping("/getBusinessByUserId")
    public Result getBusinessByUserId(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer userid = (Integer) map.get("userid");
        return  businessService.getBusinessByUserId(userid);
    }

    /** 前台商家中心修改商家信息 */
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "body", dataType = "Business", name = "business", value = "", required = true)
    })
    @ApiOperation(value = "", notes = "", httpMethod = "POST")
    @PostMapping("/updateBusiness")
    public Result updateBusiness(@RequestBody Business business){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer userid = (Integer) map.get("tbUserid");
        business.setUserid(userid);
        return businessService.updateBusiness(business);
    }

    @PostMapping("/getIsType")
    public Result getIsType(){
        HttpServletRequest request = WebUtil.getRequest();
        String token = request.getHeader("token");
        Map<String, Object> map = JwtUtil.decodeJWT(token);
        Integer is_type = (Integer) map.get("is_type");
        return new Result(200,"成功",is_type);
    }

    @PostMapping("/upload")
    @ResponseBody
    public Result upload(MultipartFile file) throws Exception{
        String path = UploadUtil.ossUpload(file);
        System.out.println(path);
        return new Result(200,"上传成功",path);
    }

    public static void main(String[] args) {
        Map<String, Object> map = JwtUtil.decodeJWT("eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VySW5mbyI6eyJpc190eXBlIjowLCJ0YlVzZXJpZCI6MX0sImlzcyI6IndhbmdwZW5nIiwiZXhwIjoxNjc4MTA3NTc3LCJpYXQiOjE2NzgxMDAzNzd9.Ey2gLfNYHSs8jwegxk4ew4YNxmycFYzeD-ghAXcsTkc");
        System.out.println("map = " + map);
    }
}
